package com.ruoyi.web.controller.prorepairengineers;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.domain.MtContact;
import com.ruoyi.domain.ProRepairEngineers;
import com.ruoyi.service.IProRepairEngineersService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 维保维修工程水/电派工单项目Controller
 * 
 * @author ruoyi
 * @date 2020-07-08
 */
@Controller
@RequestMapping("/ruoyi/engineers")
public class ProRepairEngineersController extends BaseController
{
    private String prefix = "ruoyi/engineers";

    @Autowired
    private IProRepairEngineersService proRepairEngineersService;

    @RequiresPermissions("ruoyi:engineers:view")
    @GetMapping("/engineers/{mtId}")
    public String engineers(@PathVariable("mtId") Long mtId,ModelMap mmap)
    {
        mmap.put("mtId",mtId);
        return prefix + "/engineers";
    }

    /**
     * 按Id查询维保项目合同列表
     */
    @RequiresPermissions("ruoyi:contact:list")
    @PostMapping("/lists/{mtId}")
    @ResponseBody
    public TableDataInfo lists(@PathVariable("mtId") Long mtId)
    {
        startPage();
        List<ProRepairEngineers> lists = proRepairEngineersService.selectProRepairEngineersListById(mtId);
        return getDataTable(lists);
    }

    /**
     * 查询维保维修工程水/电派工单项目列表
     */
    @RequiresPermissions("ruoyi:engineers:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProRepairEngineers proRepairEngineers)
    {
        startPage();
        List<ProRepairEngineers> list = proRepairEngineersService.selectProRepairEngineersList(proRepairEngineers);
        return getDataTable(list);
    }

    /**
     * 导出维保维修工程水/电派工单项目列表
     */
    @RequiresPermissions("ruoyi:engineers:export")
    @Log(title = "维保维修工程水/电派工单项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProRepairEngineers proRepairEngineers)
    {
        List<ProRepairEngineers> list = proRepairEngineersService.selectProRepairEngineersList(proRepairEngineers);
        ExcelUtil<ProRepairEngineers> util = new ExcelUtil<ProRepairEngineers>(ProRepairEngineers.class);
        return util.exportExcel(list, "维保维修工程");
    }

    /**
     * 新增维保维修工程水/电派工单项目
     */
    @GetMapping("/add/{mtId}")
    public String add(@PathVariable("mtId") Long mtId,ModelMap mmap )
    {
        mmap.put("mtId",mtId);
        return prefix + "/add";
    }

    /**
     * 新增保存维保维修工程水/电派工单项目
     */
    @RequiresPermissions("ruoyi:engineers:add")
    @Log(title = "维保维修工程水/电派工单项目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("file") MultipartFile file,
                              @RequestParam("file1") MultipartFile file1,
                              @RequestParam("file2") MultipartFile file2,
                              ProRepairEngineers proRepairEngineers)throws IOException
    {

        // 上传文件路径
        String filePath = Global.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        String fileName1 = FileUploadUtils.upload(filePath, file1);
        String fileName2 = FileUploadUtils.upload(filePath, file2);
//        proRepairEngineers.setmDate(new Date());
        proRepairEngineers.setmPriceSheet(fileName);
        proRepairEngineers.setmReceipt(fileName1);
        proRepairEngineers.setmJobOrder(fileName2);

        return toAjax(proRepairEngineersService.insertProRepairEngineers(proRepairEngineers));
    }

    /**
     * 新增保存维保维修工程水/电派工单项目
     */
    @RequiresPermissions("ruoyi:engineers:add")
    @Log(title = "维保维修工程水/电派工单项目", businessType = BusinessType.INSERT)
    @PostMapping("/addone")
    @ResponseBody
    public AjaxResult addSaveone(@RequestParam("file") MultipartFile file,ProRepairEngineers proRepairEngineers)throws IOException
    {

        // 上传文件路径
        String filePath = Global.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
//        proRepairEngineers.setmDate(new Date());
        proRepairEngineers.setmPriceSheet(fileName);
        return toAjax(proRepairEngineersService.insertProRepairEngineers(proRepairEngineers));
    }

    /**
     * 新增保存维保维修工程水/电派工单项目
     */
    @RequiresPermissions("ruoyi:engineers:add")
    @Log(title = "维保维修工程水/电派工单项目", businessType = BusinessType.INSERT)
    @PostMapping("/addtwo")
    @ResponseBody
    public AjaxResult addSavetwo(@RequestParam("file1") MultipartFile file1,ProRepairEngineers proRepairEngineers)throws IOException
    {

        // 上传文件路径
        String filePath = Global.getUploadPath();
        // 上传并返回新文件名称
        String fileName1 = FileUploadUtils.upload(filePath, file1);
//        proRepairEngineers.setmDate(new Date());
        proRepairEngineers.setmReceipt(fileName1);
        return toAjax(proRepairEngineersService.insertProRepairEngineers(proRepairEngineers));
    }

    /**
     * 新增保存维保维修工程水/电派工单项目
     */
    @RequiresPermissions("ruoyi:engineers:add")
    @Log(title = "维保维修工程水/电派工单项目", businessType = BusinessType.INSERT)
    @PostMapping("/addthree")
    @ResponseBody
    public AjaxResult addSavethree(@RequestParam("file2") MultipartFile file2,ProRepairEngineers proRepairEngineers)throws IOException
    {

        // 上传文件路径
        String filePath = Global.getUploadPath();
        // 上传并返回新文件名称
        String fileName2 = FileUploadUtils.upload(filePath, file2);
//        proRepairEngineers.setmDate(new Date());
        proRepairEngineers.setmJobOrder(fileName2);
        return toAjax(proRepairEngineersService.insertProRepairEngineers(proRepairEngineers));
    }

    /**
     * 新增保存维保维修工程水/电派工单项目
     */
    @RequiresPermissions("ruoyi:engineers:add")
    @Log(title = "维保维修工程水/电派工单项目", businessType = BusinessType.INSERT)
    @PostMapping("/addfour")
    @ResponseBody
    public AjaxResult addSavefour(@RequestParam("file2") MultipartFile file2,
                                  @RequestParam("file1") MultipartFile file1,
                                  ProRepairEngineers proRepairEngineers)throws IOException
    {

        // 上传文件路径
        String filePath = Global.getUploadPath();
        // 上传并返回新文件名称
        String fileName1 = FileUploadUtils.upload(filePath, file1);
        String fileName2 = FileUploadUtils.upload(filePath, file2);
//        proRepairEngineers.setmDate(new Date());
        proRepairEngineers.setmReceipt(fileName1);
        proRepairEngineers.setmJobOrder(fileName2);
        return toAjax(proRepairEngineersService.insertProRepairEngineers(proRepairEngineers));
    }

    /**
     * 新增保存维保维修工程水/电派工单项目
     */
    @RequiresPermissions("ruoyi:engineers:add")
    @Log(title = "维保维修工程水/电派工单项目", businessType = BusinessType.INSERT)
    @PostMapping("/addfive")
    @ResponseBody
    public AjaxResult addSavefive(@RequestParam("file2") MultipartFile file2,
                                  @RequestParam("file") MultipartFile file,
                                  ProRepairEngineers proRepairEngineers)throws IOException
    {

        // 上传文件路径
        String filePath = Global.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        String fileName2 = FileUploadUtils.upload(filePath, file2);
//        proRepairEngineers.setmDate(new Date());
        proRepairEngineers.setmPriceSheet(fileName);
        proRepairEngineers.setmJobOrder(fileName2);
        return toAjax(proRepairEngineersService.insertProRepairEngineers(proRepairEngineers));
    }

    /**
     * 新增保存维保维修工程水/电派工单项目
     */
    @RequiresPermissions("ruoyi:engineers:add")
    @Log(title = "维保维修工程水/电派工单项目", businessType = BusinessType.INSERT)
    @PostMapping("/addsix")
    @ResponseBody
    public AjaxResult addSavesix(@RequestParam("file1") MultipartFile file1,
                                  @RequestParam("file") MultipartFile file,
                                  ProRepairEngineers proRepairEngineers)throws IOException
    {

        // 上传文件路径
        String filePath = Global.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        String fileName1 = FileUploadUtils.upload(filePath, file1);
//        proRepairEngineers.setmDate(new Date());
        proRepairEngineers.setmPriceSheet(fileName);
        proRepairEngineers.setmReceipt(fileName1);
        return toAjax(proRepairEngineersService.insertProRepairEngineers(proRepairEngineers));
    }

    /**
     * 修改维保维修工程水/电派工单项目
     */
    @GetMapping("/edit/{mPid}")
    public String edit(@PathVariable("mPid") Long mPid, ModelMap mmap)
    {
        ProRepairEngineers proRepairEngineers = proRepairEngineersService.selectProRepairEngineersById(mPid);
        mmap.put("proRepairEngineers", proRepairEngineers);
        return prefix + "/edit";
    }

    /**
     * 修改保存维保维修工程水/电派工单项目
     */
    @RequiresPermissions("ruoyi:engineers:edit")
    @Log(title = "维保维修工程水/电派工单项目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProRepairEngineers proRepairEngineers)
    {
        return toAjax(proRepairEngineersService.updateProRepairEngineers(proRepairEngineers));
    }


    /**
     * 修改维保维修工程水/电派工单项目----联系单/报价单页面修改上传
     */
    @GetMapping("/editmPriceSheet/{mPid}")
    public String editmPriceSheet(@PathVariable("mPid") Long mPid, ModelMap mmap)
    {
        ProRepairEngineers proRepairEngineers = proRepairEngineersService.selectProRepairEngineersById(mPid);
        mmap.put("proRepairEngineers", proRepairEngineers);
        return prefix + "/editmPriceSheet";
    }

    /**
     * 修改保存维保维修工程水/电派工单项目----联系单/报价单页面修改上传
     */
    @RequiresPermissions("ruoyi:engineers:editmPriceSheet")
    @Log(title = "维保维修工程水/电派工单项目", businessType = BusinessType.UPDATE)
    @PostMapping("/editmPriceSheet")
    @ResponseBody
    public AjaxResult editmPriceSheets(@RequestParam("file") MultipartFile file,ProRepairEngineers proRepairEngineers)throws IOException
    {
        // 上传文件路径
        String filePath = Global.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        proRepairEngineers.setmPriceSheet(fileName);
        return toAjax(proRepairEngineersService.updateProRepairEngineers(proRepairEngineers));
    }

    /**
     * 修改维保维修工程水/电派工单项目----发票/收据页面修改上传
     */
    @GetMapping("/editmReceipt/{mPid}")
    public String editmReceipt(@PathVariable("mPid") Long mPid, ModelMap mmap)
    {
        ProRepairEngineers proRepairEngineers = proRepairEngineersService.selectProRepairEngineersById(mPid);
        mmap.put("proRepairEngineers", proRepairEngineers);
        return prefix + "/editmReceipt";
    }

    /**
     * 修改保存维保维修工程水/电派工单项目----发票/收据页面修改上传
     */
    @RequiresPermissions("ruoyi:engineers:editmReceipt")
    @Log(title = "维保维修工程水/电派工单项目", businessType = BusinessType.UPDATE)
    @PostMapping("/editmReceipt")
    @ResponseBody
    public AjaxResult editmReceipts(@RequestParam("file1") MultipartFile file1,ProRepairEngineers proRepairEngineers)throws IOException
    {
        // 上传文件路径
        String filePath = Global.getUploadPath();
        // 上传并返回新文件名称
        String fileName1 = FileUploadUtils.upload(filePath, file1);
        proRepairEngineers.setmReceipt(fileName1);
        return toAjax(proRepairEngineersService.updateProRepairEngineers(proRepairEngineers));
    }

    /**
     * 修改维保维修工程水/电派工单项目----水/电派工单修改上传
     */
    @GetMapping("/editmJobOrder/{mPid}")
    public String editmJobOrder(@PathVariable("mPid") Long mPid, ModelMap mmap)
    {
        ProRepairEngineers proRepairEngineers = proRepairEngineersService.selectProRepairEngineersById(mPid);
        mmap.put("proRepairEngineers", proRepairEngineers);
        return prefix + "/editmJobOrder";
    }

    /**
     * 修改保存维保维修工程水/电派工单项目----水/电派工单修改上传
     */
    @RequiresPermissions("ruoyi:engineers:editmJobOrder")
    @Log(title = "维保维修工程水/电派工单项目", businessType = BusinessType.UPDATE)
    @PostMapping("/editmJobOrder")
    @ResponseBody
    public AjaxResult editmJobOrders(@RequestParam("file2") MultipartFile file2,ProRepairEngineers proRepairEngineers)throws IOException
    {
        // 上传文件路径
        String filePath = Global.getUploadPath();
        // 上传并返回新文件名称
        String fileName2 = FileUploadUtils.upload(filePath, file2);
        proRepairEngineers.setmJobOrder(fileName2);
        return toAjax(proRepairEngineersService.updateProRepairEngineers(proRepairEngineers));
    }

    /**
     * 删除维保维修工程水/电派工单项目
     */
    @RequiresPermissions("ruoyi:engineers:remove")
    @Log(title = "维保维修工程水/电派工单项目", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(proRepairEngineersService.deleteProRepairEngineersByIds(ids));
    }


    /**
     * 维修联系单/报价单下载
     */
    @GetMapping("downloadFile")
    public void resourceDownload(@PathVariable("mPriceSheet") String mPriceSheet, HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        // 本地资源路径
        String localPath = Global.getProfile();
        // 数据库资源地址
        String downloadPath = localPath + StringUtils.substringAfter(mPriceSheet, Constants.RESOURCE_PREFIX);
        // 下载名称
        String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, downloadName));
        FileUtils.writeBytes(downloadPath, response.getOutputStream());
    }

}
